package com.weiyi.mashangzhu.Landlord.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetHouseTitieActivity extends BaseActivity {

    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.content)
    EditText content;
    String info,id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_house_titie);
        ButterKnife.bind(this);
        Intent intent=getIntent();
        info=intent.getStringExtra("info");
        id=intent.getStringExtra("id");
        if (info.equals("post")){
            //上传房源
            if (MyApp.house_title != null&&!MyApp.house_title.equals("")) {
                name.setText(MyApp.house_title);
            }
            if (MyApp.house_content != null&&!MyApp.house_content.equals("")) {
                content.setText(MyApp.house_content);
            }
        }else {
            //修改房源
            LoadingDialog.show(this);
            OkHttpUtils.get()
                    .addParams("token", SPUtils.get(this,"token","")+"")
                    .addParams("id",id)
                    .url(API.BaseApi_L+API.GetHouseTitle)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            T.showShort(SetHouseTitieActivity.this,"网络连接失败");
                            LoadingDialog.dismiss(SetHouseTitieActivity.this);
                        }

                        @Override
                        public void onResponse(String response) {
                            LoadingDialog.dismiss(SetHouseTitieActivity.this);
                            try {
                                JSONObject jsonObject=new JSONObject(response);
                                if (jsonObject.getInt("status")==0){
                                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                                    JSONObject jsonObject1=jsonArray.getJSONObject(0);
                                    name.setText(jsonObject1.getString("hTitle"));
                                    content.setText(jsonObject1.getString("hIntro"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });

        }

    }

    @OnClick({R.id.back, R.id.baocun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.baocun:
                if (info.equals("post")){
                    //上传房源
                    if ((name.getText() + "").equals("")) {
                        T.showShort(this, "房屋标题不能为空");
                    } else {
                        if ((content.getText() + "").equals("")) {
                            T.showShort(this, "房屋介绍不能为空");
                        } else {
                            MyApp.house_title = name.getText() + "";
                            MyApp.house_content = content.getText() + "";
                            Intent intent = new Intent();
                            intent.putExtra("info", "完成");
                            intent.putExtra("name", name.getText() + "");
                            intent.putExtra("content", content.getText() + "");
                            setResult(0x002, intent);
                            finish();
                        }
                    }

                }else {
                    //修改房源
                    LoadingDialog.show(this);
                    OkHttpUtils.get()
                            .addParams("token",SPUtils.get(this,"token","")+"")
                            .addParams("id",id)
                            .addParams("hTitle",name.getText()+"")
                            .addParams("hIntro",content.getText()+"")
                            .url(API.BaseApi_L+API.UpdateHouseTitle)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Request request, Exception e) {
                                    LoadingDialog.dismiss(SetHouseTitieActivity.this);
                                    T.showShort(SetHouseTitieActivity.this,"网络连接失败");
                                }

                                @Override
                                public void onResponse(String response) {
                                    LoadingDialog.dismiss(SetHouseTitieActivity.this);
                                    try {
                                        JSONObject jsonObject=new JSONObject(response);
                                        if (jsonObject.getInt("status")==0){
                                            T.showShort(SetHouseTitieActivity.this,jsonObject.getString("msg") + " 后台审核中...");
                                            finish();
                                        }else {
                                            T.showShort(SetHouseTitieActivity.this,jsonObject.getString("msg"));
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }


                                }
                            });
                }

                break;
        }
    }
}
